<template>
  <div class="app-container">
    <div class="filter-container">
      <el-card>
        <el-form ref="searchDbform" :inline="true" :model="searchform" class="demo-form-inline" size="small">
          <el-form-item label="数据库地址" prop="DbAddress">
            <el-input v-model="searchDbform.DbAddress" placeholder="请输入数据库地址" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="数据库名称" prop="DbName">
            <el-input v-model="searchDbform.DbName" placeholder="请输入数据库名称" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="用户名" prop="DbUserName">
            <el-input v-model="searchDbform.DbUserName" placeholder="请输入用户名" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="访问密码" prop="DbPassword">
            <el-input v-model="searchDbform.DbPassword" placeholder="请输入访问密码" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="数据库类型" prop="DbType">
            <el-select v-model="searchDbform.DbType" clearable placeholder="请选数据库类型">
              <el-option
                v-for="item in selectDbTypes"
                :key="item.Id"
                :label="item.Title"
                :value="item.Id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="数据库端口" prop="DbPort">
            <el-input v-model="searchDbform.DbPort" placeholder="请输入数据库端口" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleDbConn()">链接</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <el-card>
      <div class="list-btn-container">
        <el-form ref="codeform" :inline="true" :rules="rules" :model="codeform" class="demo-form-inline" size="small">
          <el-button type="default" icon="el-icon-refresh" size="small" @click="loadTableData()">刷新</el-button>
          <el-form-item label="数据库">
            <el-tooltip class="item" effect="dark" content="默认为系统访问数据库" placement="top">
              <el-select v-model="searchform.DbName" clearable placeholder="请选择" @change="handleShowTable">
                <el-option
                  v-for="item in selectedDataBase"
                  :key="item.Id"
                  :label="item.DbName"
                  :value="item.DbName"
                />
              </el-select>
            </el-tooltip>
          </el-form-item>
          <el-form-item label="数据库表名">
            <el-input v-model="searchform.tableName" clearable placeholder="输入要查询的表名" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch()">查询</el-button>
          </el-form-item>
          <el-form-item label="项目命名空间：" prop="baseSpace">
            <el-tooltip class="item" effect="dark" content="系统会根据项目命名空间自动生成IService、Service、Models等子命名空间" placement="top">
              <el-input v-model="codeform.baseSpace" clearable placeholder="如Yuebon.WMS" />
            </el-tooltip>
          </el-form-item>
          <el-form-item label="去掉表名前缀：">
            <el-tooltip class="item" effect="dark" content="表名直接变为类名，去掉表名前缀。多个前缀用“;”隔开和结束" placement="top">
              <el-input v-model="codeform.replaceTableNameStr" clearable width="300" placeholder="多个前缀用“;”隔开" />
            </el-tooltip>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="iconfont icon-code" @click="handleGenerate()">生成代码</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        ref="gridtable"
        v-loading="tableloading"
        :data="tableData"
        border
        stripe
        highlight-current-row
        style="width: 100%"
        :default-sort="{prop: 'TableName', order: 'ascending'}"
        @select="handleSelectChange"
        @select-all="handleSelectAllChange"
        @sort-change="handleSortChange"
      >
        <el-table-column
          type="selection"
          width="30"
        />
        <el-table-column
          prop="TableName"
          label="表名"
          sortable="custom"
          width="380"
        />
        <el-table-column
          prop="Description"
          label="表描述"
        />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="priviewCode(scope.row)">预览代码</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          background
          :current-page="pagination.currentPage"
          :page-sizes="[5,10,20,50,100, 200, 300, 400]"
          :page-size="pagination.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.pageTotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>


	<el-dialog title="代码生成在线预览" :visible.sync="outerVisible" :fullscreen=true>
		<el-tabs v-model="activeName" @tab-click="handleClick">
			<el-tab-pane label="Controllers" name="controllers"><pre>{{ priviewObj.controllers }}</pre></el-tab-pane>
			<el-tab-pane label="IRepository" name="iRepository"><pre>{{ priviewObj.iRepository }}</pre></el-tab-pane>
			<el-tab-pane label="Repository" name="repository"><pre>{{ priviewObj.repository }}</pre></el-tab-pane>
			<el-tab-pane label="IService" name="iService"><pre>{{ priviewObj.iService }}</pre></el-tab-pane>
			<el-tab-pane label="Service" name="service"><pre>{{ priviewObj.service }}</pre></el-tab-pane>
			<el-tab-pane label="Model" name="model"><pre>{{ priviewObj.model }}</pre></el-tab-pane>
			<el-tab-pane label="InputDto" name="inputDto"><pre>{{ priviewObj.inputDto }}</pre></el-tab-pane>
			<el-tab-pane label="OutputDto" name="outputDto"><pre>{{ priviewObj.outputDto }}</pre></el-tab-pane>
			<el-tab-pane label="VueViews" name="vueViews"><pre>{{ priviewObj.vueViews }}</pre></el-tab-pane>
			<el-tab-pane label="DtoProfile" name="dtoProfile"><pre>{{ priviewObj.dtoProfile }}</pre></el-tab-pane>
		</el-tabs>
	</el-dialog>
  </div>
</template>

<style scoped>
.priview-code{
	height: 600px !important;
}
</style>

<script>
import { createGetDBConn, codeGetDBList, codeGetTableList, codeGenerator,codePriview } from '@/api/developers/toolsservice'
import { downloadFile } from '@/utils/index'
import { Loading } from 'element-ui'

import defaultSettings from '@/settings'
export default {
  name: 'CodeGenerator',
  data() {
    return {
		outerVisible:false,
		activeName:'controllers',
		priviewObj:{
			controllers:'',
			iRepository:'',
			iService:'',
			model:'',
			inputDto:'',
			outputDto:'',
			repository:'',
			service:'',
			vueViews:'',
			dtoProfile:''

		},
		searchDbform: {
			DbName: '',
			DbAddress: '',
			DbPort: '1433',
			DbUserName: '',
			DbPassword: '',
			DbType: ''
		},
		searchform: {
			DbName: '',
			tableName: ''
		},
		codeform: {
			baseSpace: '',
			replaceTableNameStr: ''
		},
		rules: {
			baseSpace: [
			{ required: true, message: '请输入项目名称', trigger: 'blur' },
			{ min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
			],
			replaceTableNameStr: [
			{ min: 0, max: 50, message: '长度小于50个字符', trigger: 'blur' }
			]
		},
		selectDbTypes: [{
			Id: 'SqlServer',
			Title: 'SqlServer'
		}, {
			Id: 'MySql',
			Title: 'MySql'
		}],
		tableData: [],
		tableloading: true,
		pagination: {
			currentPage: 1,
			pagesize: 20,
			pageTotal: 0
		},
		sortableData: {
			order: '',
			sort: ''
		},
		currentSelected: [],
		selectedDataBase: []
    }
  },
  created() {
    this.pagination.currentPage = 1
    this.loadData()
    this.loadTableData()
  },
  methods: {
	handleClick(){

	},
    priviewCode:function(row){
		var loadop = {
			lock: true,
			text: '正在生成预览代码...',
			spinner: 'el-icon-loading',
			background: 'rgba(0, 0, 0, 0.7)'
		}
		const pageLoading = Loading.service(loadop)
		var currentTables = row.TableName;
		
		var seachdata = {
			'tables': currentTables,
			'baseSpace': 'HaojieLi',
			'replaceTableNameStr': ''
		}
		var that = this;
		codePriview(seachdata).then(res => {
			
			that.priviewObj = JSON.parse(res.ResData);
			
			if (res.Success) {
				this.$message({
					message: '在线预览代码生成完成，即将打开预览页面！',
					type: 'success'
				})
				that.outerVisible = true;
			} else {
				this.$message({
					message: res.ErrMsg,
					type: 'error'
				})
			}
			pageLoading.close()
		}).catch(erre => {
			pageLoading.close()
		})
    },
    loadData: function() {
		codeGetDBList().then(res => {
			this.selectedDataBase = res.ResData
		})
    },
    /**
     * 加载页面table数据
     */
    loadTableData: function() {
      if (this.searchform.dataBaseName !== '') {
        this.tableloading = true
        var seachdata = {
          'CurrenetPageIndex': this.pagination.currentPage,
          'PageSize': this.pagination.pagesize,
          'Keywords': this.searchform.tableName,
          'EnCode': this.searchform.DbName,
          'Order': this.sortableData.order,
          'Sort': this.sortableData.sort
        }
        codeGetTableList(seachdata).then(res => {
          this.tableData = res.ResData.Items
          this.pagination.pageTotal = res.ResData.TotalItems
          this.tableloading = false
        })
      }
    },
    /**
     * 点击查询
     */
    handleSearch: function() {
      this.pagination.currentPage = 1
      this.loadTableData()
    },
    handleShowTable: function() {
      this.pagination.currentPage = 1
      this.loadTableData()
    },
    handleDbConn: function() {
      var dataInfo = {
        DbAddress: this.searchDbform.DbAddress,
        DbPort: this.searchDbform.DbPort,
        DbName: this.searchDbform.DbName,
        DbUserName: this.searchDbform.DbUserName,
        DbPassword: this.searchDbform.DbPassword,
        DbType: this.searchDbform.DbType
      }
      createGetDBConn(dataInfo).then(res => {
        this.selectedDataBase = res.ResData
        this.searchform.DbName = this.searchDbform.DbName
      })
      this.pagination.currentPage = 1
      this.loadTableData()
    },
    /**
     * 点击生成服务端代码
     */
    handleGenerate: async function() {
      if (this.currentSelected.length === 0) {
        this.$alert('请先选择要生成代码的数据表', '提示')
        return false
      } else {
        this.$refs['codeform'].validate((valid) => {
          if (valid) {
            var loadop = {
              lock: true,
              text: '正在生成代码...',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            }
            const pageLoading = Loading.service(loadop)
            var currentTables = ''
            this.currentSelected.forEach(element => {
              currentTables += element.TableName + ','
            })
            var seachdata = {
              'tables': currentTables,
              'baseSpace': this.codeform.baseSpace,
              'replaceTableNameStr': this.codeform.replaceTableNameStr
            }
            codeGenerator(seachdata).then(res => {
              if (res.Success) {
                downloadFile(defaultSettings.fileUrl + res.ResData[0], res.ResData[1])
                this.$message({
                  message: '恭喜你，代码生成完成！',
                  type: 'success'
                })
              } else {
                this.$message({
                  message: res.ErrMsg,
                  type: 'error'
                })
              }
              pageLoading.close()
            }).catch(erre => {
              pageLoading.close()
            })
          } else {
            return false
          }
        })
      }
    },
    /**
     * 当表格的排序条件发生变化的时候会触发该事件
     */
    handleSortChange: function(column) {
      this.sortableData.sort = column.prop
      if (column.order === 'ascending') {
        this.sortableData.order = 'asc'
      } else {
        this.sortableData.order = 'desc'
      }
      this.loadTableData()
    },
    /**
     * 当用户手动勾选checkbox数据行事件
     */
    handleSelectChange: function(selection, row) {
      this.currentSelected = selection
    },
    /**
     * 当用户手动勾选全选checkbox事件
     */
    handleSelectAllChange: function(selection) {
      this.currentSelected = selection
    },
    /**
     * 选择每页显示数量
     */
    handleSizeChange(val) {
      this.pagination.pagesize = val
      this.pagination.currentPage = 1
      this.loadTableData()
    },
    /**
     * 选择当页面
     */
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.loadTableData()
    }
  }
}
</script>
